home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 398 / 398.xpi / chrome / forecastfox.jar / content / icons / icons.js < prev    next >
Text File  |  2010-02-04  |  6KB  |  171 lines

  1. /*------------------------------------------------------------------------------
  2.   Copyright (c) 2008 Ensolis, LLC. All Rights Reserved.
  3.   ----------------------------------------------------------------------------*/
  4.  
  5. var gIcons = null;
  6.  
  7. function iconsLoad()
  8. {
  9.   gIcons = new Icons();
  10.   gIcons.start();
  11. }
  12.  
  13. function iconsUnload()
  14. {
  15.   gIcons.stop();
  16.   gIcons = null;
  17. }
  18.  
  19. function Icons() {}
  20. Icons.prototype = {
  21.   _bundle: null,
  22.   _manager: null,
  23.   _name: null,
  24.   _url: null,
  25.   _progress: null,
  26.   _description: null,
  27.   _install: null,
  28.   _persist: null,
  29.   _file: null,
  30.   _cancel: null,
  31.   
  32.   start: function Icons_start()
  33.   {
  34.     //setup components
  35.     this._bundle = document.getElementById("ff-bundle-icons");    
  36.     this._manager = Components.classes["@ensolis.com/forecastfox/manager-service;1"].getService(Components.interfaces.ffIManagerService);  
  37.     
  38.     //set display
  39.     this._name = document.getElementById("ff-icons-name");
  40.     this._url = document.getElementById("ff-icons-url");
  41.     this._progress = document.getElementById("ff-icons-progress");
  42.     this._description = document.getElementById("ff-icons-description");
  43.     this._install = document.getElementById("ff-button-install");
  44.     this._cancel = document.getElementById("ff-button-cancel");  
  45.     var params = window.arguments[0].QueryInterface(
  46.                           Components.interfaces.nsIDialogParamBlock);
  47.     this._name.value = params.GetString(0);
  48.     this._url.value = params.GetString(1);
  49.     this._url.tooltiptext = this._url.value;
  50.   },
  51.   
  52.   stop: function Icons_stop()
  53.   {
  54.     //stop download or install
  55.     try {    
  56.       if (this._persist && this._persist.currentState < 3) 
  57.         this._persist.cancelSave(); 
  58.       if (this._file && this._file.exists())
  59.         this._file.remove(false);
  60.     } catch(e) {} 
  61.              
  62.     //remove variables
  63.     this._url = null;
  64.     this._name = null;
  65.     this._manager = null;
  66.     this._bundle = null;
  67.     this._progress = null;
  68.     this._description = null;
  69.     this._install = null;
  70.     this._persist = null;
  71.     this._file = null;    
  72.     this._cancel = null;
  73.   },
  74.   
  75.   install: function Icons_install()
  76.   {
  77.     //set display
  78.     this._install.setAttribute("disabled", "true");
  79.     this._progress.removeAttribute("collapsed");
  80.     this._progress.setAttribute("value", "0");    
  81.     this._progress.setAttribute("mode", "determined");    
  82.     this._description.value = this._bundle.getString("ff.icons.download");
  83.     window.sizeToContent();
  84.     
  85.     //convert url to uri and get file
  86.     var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);  
  87.     var uri = ios.newURI(this._url.value, null, null);
  88.     var fileName = uri.QueryInterface(Components.interfaces.nsIURL).fileName;
  89.     this._file = this._manager.disk.get(fileName, Components.interfaces.ffIDiskService.TYPE_TEMP);
  90.  
  91.     //persist the file
  92.     var flags = 0x02 | 0x32;
  93.     this._persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);                       
  94.     this._persist.persistFlags |= flags;
  95.     this._persist.progressListener = this;
  96.     this._persist.saveURI(uri, null, null, null, null, this._file);      
  97.   },
  98.   
  99.   _installItem: function Icons__installItem()
  100.   {
  101.     //perform install
  102.     try {
  103.       var rv = this._manager.icons.setItem(this._file);
  104.     } catch(e) {
  105.       this._description.value = this._bundle.getString("ff.icons.failed");
  106.       this._manager.disk.log("Failed to install icon pack.", e, null);
  107.     }  
  108.         
  109.     //set progress
  110.     this._progress.setAttribute("value", "100");    
  111.     this._progress.setAttribute("mode", "determined"); 
  112.     this._cancel.label = this._bundle.getString("ff.icons.close");
  113.       
  114.     //set description
  115.     if (rv) {
  116.       this._description.value = this._bundle.getString("ff.icons.success");
  117.       window.setTimeout(window.close, 0);        
  118.     } else 
  119.       this._description.value = this._bundle.getString("ff.icons.failed");
  120.       
  121.     //remove persisted file
  122.     try {
  123.       this._file.remove(false);
  124.     } catch(e) {} 
  125.   },
  126.     
  127.   ///////////////////////////
  128.   // nsIWebProgressListener  
  129.   onLocationChange: function Icons_onLocationChange(webProgress, request, location) {},
  130.   onSecurityChange: function Icons_onSecurityChange(webProgress, request, state) {},
  131.   onStatusChange: function Icons_onStatusChange(webProgress, request, status, message) {},
  132.   onProgressChange: function Icons_onProgressChange(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress ) 
  133.   {
  134.     if (maxTotalProgress > 0) {
  135.       var percentage = (curTotalProgress * 100) / maxTotalProgress;
  136.       this._progress.value = percentage;
  137.     }  
  138.   },  
  139.   onStateChange: function Icons_onStateChange(webProgress, request, stateFlags, status) 
  140.   {
  141.     var stateDone = Components.interfaces.nsIWebProgressListener.STATE_STOP;
  142.     
  143.     //download complete
  144.     if (stateDone == (stateFlags & stateDone)) {
  145.       var comp = this;
  146.       var channel = request.QueryInterface(Components.interfaces.nsIHttpChannel);
  147.       if (channel.responseStatus == 200) {  
  148.         this._progress.removeAttribute("value");    
  149.         this._progress.setAttribute("mode", "undetermined");
  150.         this._description.value = this._bundle.getString("ff.icons.install");
  151.         window.setTimeout(function() { comp._installItem(); }, 0);
  152.       } else {
  153.         this._progress.setAttribute("value", "100");    
  154.         this._progress.setAttribute("mode", "determined"); 
  155.         this._cancel.label = this._bundle.getString("ff.icons.close");
  156.         this._description.value = this._bundle.getString("ff.icons.persist");      
  157.       }
  158.     }
  159.   },
  160.  
  161.   ///////////////////////////
  162.   // nsISupports    
  163.   QueryInterface: function Icons_QueryInterface(iid)
  164.   {        
  165.     if (!iid.equals(Components.interfaces.nsIWebProgressListener) &&
  166.         !iid.equals(Components.interfaces.nsISupports))
  167.       throw Components.results.NS_ERROR_NO_INTERFACE;
  168.     return this;
  169.   }
  170. };
  171.